package com.itheima.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * response响应字符数据：设置字符数据的响应体
 */

@WebServlet("/ResponseDemo3")
public class ResponseDemo3 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应数据格式和数据的编码，放在获取输出流对象之前（很常用）
        response.setContentType("text/html;charset=utf-8");
        //1.获取输出流对象
        PrintWriter writer = response.getWriter();//打印流
        //2.设置响应的数据格式

//        response.setHeader("content-type","text/html");
        //3.通过打印流的write方法输出数据
        writer.write("aaa");//输出字符数据
        //输出html数据
        writer.write("<h1>aaa</h1>");//tomcat默认向浏览器发送的text文本信息，要输出html信息，就要设置响应的数据格式

        //4.输出中文出现乱码，是因为输出流默认使用的ISO-8859-1的编码，我们要设置编码为UTF-8


        writer.write("你好");


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}